package com.java.database;

import java.sql.*;

/**
 * @author wangning
 * @create 2021-04-26 11:57
 */
public class TestOracleJdbc {
	public static void main(String[] args) throws SQLException {
		Connection connect = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			Class.forName("oracle.jdbc.OracleDriver");
			//jdbc:oracle:thin:@10.150.4.66:1521/pdb
			connect = DriverManager.getConnection("jdbc:oracle:thin:@10.150.4.66:1521/pdb", "sso", "sso");
			System.out.println("connect = " + connect);
//			statement = connect.prepareStatement("select * from aweb_user t");
			statement = connect.prepareStatement(
					"select * from aweb_user t where " +
					"t.ID = decode(NVL(?,''),'',t.ID,?)" +
					"and t.NAME = decode(NVL(?,''),'',t.NAME,?)");
			//select * from aweb_user t where t.ID = decode(NVL(?,''),'',t.ID,?) and t.NAME = decode(NVL(?,''),'',t.NAME,?)
			//select * from aweb_user t where ('$(param)' is null or t.id = '$(param)')
			System.out.println("statement = " + statement.getMetaData());
			statement.setString(1, null);
			statement.setString(2, null);
			statement.setString(3, null);
			statement.setString(4, null);

			resultSet = statement.executeQuery();        //执行查询语句
			//第五步：处理结果集
			while (resultSet.next()) {
				String id = resultSet.getString("id");
				String name = resultSet.getString("name");
				System.out.println(id + ":" + name);  //打印输出结果集
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null) resultSet.close();
				if (statement != null) statement.close();
				if (connect != null) connect.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
